package com.example.demo.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import org.json.JSONObject;

public class gongju {
    public static JSONObject str2json(String s) {
        String str = s;
        str = str.replaceAll("\\{", "");
        str = str.replaceAll("\\}", "");

        System.out.println(str);
        List<String> arr = Arrays.asList(str.split(", "));
        JSONObject json = new JSONObject();
        for (int i = 0; i < arr.size(); i++) {
            String s2 = arr.get(i);
            List<String> arr2 = Arrays.asList(s2.split("="));
            json.put(arr2.get(0), arr2.get(1));
        }

        return json;
    }

    // Object的数组转List
    public static List<JSONObject> obj2list(Object[] arr) {
        List<JSONObject> list = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            JSONObject obj = gongju.str2json(arr[i].toString());
            list.add(obj);
        }
        return list;
    }

    // 从Object里获取字段
    public static String ogetstr(Object o, String t) {
        return str2json(o.toString()).getString(t);
    }

    public static int ogetint(Object o, String t) {
        return Integer.valueOf(str2json(o.toString()).getString(t));
    }

    public static int suiji(int min, int max) {
        Random random = new Random();
        int randomNumber = random.nextInt(max - min + 1) + min; // 包括min和max
        return randomNumber;
    }
}
